package com.csmz.mybookreadernew.UI.Fragment;

import android.os.Handler;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;

import com.csmz.mybookreadernew.Adapter.FragmentAdapter;
import com.csmz.mybookreadernew.Host.RssHost;
import com.csmz.mybookreadernew.R;
import com.csmz.mybookreadernew.RssUtils.RssFeed;
import com.csmz.mybookreadernew.RssUtils.RssFeed_SAXParser;
import com.csmz.mybookreadernew.UI.Fragment.MyReader.MyNewsReadFragment_00;
import com.csmz.mybookreadernew.UI.Fragment.MyReader.MyNewsReadFragment_01;
import com.csmz.mybookreadernew.UI.Fragment.MyReader.MyNewsReadFragment_02;
import com.csmz.mybookreadernew.UI.Fragment.MyReader.MyNewsReadFragment_03;
import com.csmz.mybookreadernew.UI.Fragment.MyReader.MyNewsReadFragment_04;
import com.csmz.mybookreadernew.UI.Fragment.MyReader.MyNewsReadFragment_05;
import com.csmz.mybookreadernew.UI.Fragment.MyReader.MyNewsReadFragment_06;
import com.csmz.mybookreadernew.UI.Fragment.MyReader.MyNewsReadFragment_07;
import com.csmz.mybookreadernew.UI.Fragment.MyReader.MyNewsReadFragment_08;
import com.csmz.mybookreadernew.UI.Fragment.MyReader.MyNewsReadFragment_09;
import com.king.base.BaseFragment;

import org.xml.sax.SAXException;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;

public class MyReadFragment extends BaseFragment {
    private ViewPager viewPager;
    private TabLayout tabLayout;
    private FragmentAdapter adapter;
    private List<Fragment> fragments;


    private RssFeed feed01=null;
    private RssFeed feed02=null;
    private RssFeed feed03=null;
    private RssFeed feed04=null;
    private RssFeed feed05=null;
    private RssFeed feed06=null;
    private RssFeed feed07=null;
    private RssFeed feed08=null;
    private RssFeed feed09=null;
    private RssFeed feed10=null;

    private MyNewsReadFragment_00 myNewsFragment00;
    private MyNewsReadFragment_01 myNewsFragment01;
    private MyNewsReadFragment_02 myNewsFragment02;
    private MyNewsReadFragment_03 myNewsFragment03;
    private MyNewsReadFragment_04 myNewsFragment04;
    private MyNewsReadFragment_05 myNewsFragment05;
    private MyNewsReadFragment_06 myNewsFragment06;
    private MyNewsReadFragment_07 myNewsFragment07;
    private MyNewsReadFragment_08 myNewsFragment08;
    private MyNewsReadFragment_09 myNewsFragment09;

    private Handler uiHandler=new Handler();
    private DownLoadThread downLoadThread=new DownLoadThread();
    private boolean start=false;

    @Override
    public int inflaterRootView() {
        return R.layout.content_main;
    }


    class DownLoadThread extends Thread{
        @Override
        public void run() {
            start=true;
            try {
                feed01 = new RssFeed_SAXParser().getFeed(RssHost.RSS_URL_TX_GN);
                feed02= new RssFeed_SAXParser().getFeed(RssHost.RSS_URL_TX_CJ);
                feed03= new RssFeed_SAXParser().getFeed(RssHost.RSS_URL_TX_IT);
                feed04= new RssFeed_SAXParser().getFeed(RssHost.RSS_URL_TX_HLW);
                feed05= new RssFeed_SAXParser().getFeed(RssHost.RSS_URL_TX_TX);
                feed06= new RssFeed_SAXParser().getFeed(RssHost.RSS_URL_TX_CYCX);
                feed07= new RssFeed_SAXParser().getFeed(RssHost.RSS_URL_TX_YDHL);
                feed08= new RssFeed_SAXParser().getFeed(RssHost.RSS_URL_TX_DZSW);
                feed09= new RssFeed_SAXParser().getFeed(RssHost.RSS_URL_TX_SJWL);
                feed10= new RssFeed_SAXParser().getFeed(RssHost.RSS_URL_TX_WLYX);
            } catch (ParserConfigurationException e) {
                e.printStackTrace();
            } catch (SAXException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            Runnable runnable=new Runnable() {
                @Override
                public void run() {
                    MyNewsReadFragment_00.getRssFeed(getActivity(),feed01);
                    MyNewsReadFragment_01.getRssFeed(getActivity(),feed02);
                    MyNewsReadFragment_02.getRssFeed(getActivity(),feed03);
                    MyNewsReadFragment_03.getRssFeed(getActivity(),feed04);
                    MyNewsReadFragment_04.getRssFeed(getActivity(),feed05);
                    MyNewsReadFragment_05.getRssFeed(getActivity(),feed06);
                    MyNewsReadFragment_06.getRssFeed(getActivity(),feed07);
                    MyNewsReadFragment_07.getRssFeed(getActivity(),feed08);
                    MyNewsReadFragment_08.getRssFeed(getActivity(),feed09);
                    MyNewsReadFragment_09.getRssFeed(getActivity(),feed10);
                }
            };
            uiHandler.post(runnable);
        }
    }


    @Override
    public void initUI() {
        if (!start){
            downLoadThread.start();
        }
        initView();
    }

    private void initView() {

        viewPager=findView(R.id.main_viewpager);
        tabLayout=findView(R.id.tab_layout);
        fragments = new ArrayList<>();
        myNewsFragment00 = new MyNewsReadFragment_00();
        myNewsFragment01 = new MyNewsReadFragment_01();
        myNewsFragment02 = new MyNewsReadFragment_02();
        myNewsFragment03 = new MyNewsReadFragment_03();
        myNewsFragment04 = new MyNewsReadFragment_04();
        myNewsFragment05 = new MyNewsReadFragment_05();
        myNewsFragment06 = new MyNewsReadFragment_06();
        myNewsFragment07 = new MyNewsReadFragment_07();
        myNewsFragment08 = new MyNewsReadFragment_08();
        myNewsFragment09 = new MyNewsReadFragment_09();
        fragments.add(myNewsFragment00);
        fragments.add(myNewsFragment01);
        fragments.add(myNewsFragment02);
        fragments.add(myNewsFragment03);
        fragments.add(myNewsFragment04);
        fragments.add(myNewsFragment05);
        fragments.add(myNewsFragment06);
        fragments.add(myNewsFragment07);
        fragments.add(myNewsFragment08);
        fragments.add(myNewsFragment09);
        adapter = new FragmentAdapter(getChildFragmentManager(),fragments);
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.getTabAt(0).setText("国内新闻");
        tabLayout.getTabAt(1).setText("财经新闻");
        tabLayout.getTabAt(2).setText("IT新闻");
        tabLayout.getTabAt(3).setText("互联网新闻");
        tabLayout.getTabAt(4).setText("通信新闻");
        tabLayout.getTabAt(5).setText("创业创新");
        tabLayout.getTabAt(6).setText("移动互联");
        tabLayout.getTabAt(7).setText("电子商务");
        tabLayout.getTabAt(8).setText("社交网络");
        tabLayout.getTabAt(9).setText("网络游戏");
    }

    @Override
    public void initData() {

    }

    @Override
    public void addListeners() {

    }
}
